<?php
namespace Home\Controller;
use Think\Controller;
class LockController extends Controller {
	public function _initialize($type=NULL){
		if($type==1){//独占堵塞
			$this->type=LOCK_EX;
		}
		else if($type==2){//独占非堵塞
			$this->type=LOCK_EX|LOCK_NB;
		}
		else if($type==3){//共享堵塞
			$this->type=LOCK_SH;
		}
		else if($type==4){//共享非堵塞
			$this->type=LOCK_SH|LOCK_NB;
		}
		else{//默认独占堵塞
			$this->type=LOCK_EX;
		}
	}
	public function openFile($dir){//打开文件
		$fp=fopen($dir,'a+');
		return $fp;
	}
	public function closeFile($fp){//关闭文件
		$fp=fclose($fp);
		return $fp;
	}
	public function getLock($fp){//获取锁
		$LOCK=flock($fp,$this->type);
		if(!$LOCK){//锁定失败
			return false;
		}
		else{
			return true;
		}
	}
	public function releaseLock($fp){//释放锁
		$LOCK=flock($fp,LOCK_UN);
		if(!$LOCK){//关闭锁失败
			return false;
		}
		else{
			fclose($fp);
			return true;
		}
	}
}
?>
